package com.dpp.service.impl;

import com.dpp.entity.CouponBook;
import com.dpp.entity.Invite;
import com.dpp.entity.InviteRecord;
import com.dpp.mapper.CouponBookMapper;
import com.dpp.mapper.CouponMapper;
import com.dpp.mapper.InviteMapper;
import com.dpp.mapper.InviteRecordMapper;
import com.dpp.result.Result;
import com.dpp.service.InviteService;
import com.dpp.service.ProductInfoService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

@Service
public class InviteServiceImpl implements InviteService {
    @Autowired
    private  InviteMapper inviteMapper;
    @Autowired
    private InviteRecordMapper inviteRecordMapper;
    @Autowired
    private CouponBookMapper couponBookMapper;
    @Autowired
    private CouponMapper couponMapper;
    @Override
    public Invite getCode(String openid) {
       Invite invite= inviteMapper.selectByOpenid(openid);
       if(invite==null){
           Invite invite1 = new Invite();
           invite1.setOpenid(openid);
           invite1.setCount(Invite.COUNT);
           String pass = generateCouponCode();
           String pass1 = CheckCode(pass);
           invite1.setPass(pass1);
           invite1.setCreateTime(LocalDateTime.now());
           inviteMapper.save(invite1);
           invite1.setOpenid("");
           return invite1;
       }
        invite.setOpenid("");
        return invite;
    }

    @Override
    public Result CheckCode(String pass, String openid) {
        Invite invite= inviteMapper.selectByCode(pass);
        if(invite==null){
            return Result.error("查无此兑换码");
        }
        if(invite.getCount()<=0){
            return Result.error("兑换码次数用完了");
        }
        if(openid.equals(invite.getOpenid())){
            return Result.error("不可给自己使用");
        }

        List<InviteRecord> inviteRecord = inviteRecordMapper.selectByOpenid(openid);
        if(inviteRecord.isEmpty()){
              //说明是新用户
            extracted(openid, invite,3);
        }else{
            for (InviteRecord record : inviteRecord) {
                if(openid.equals(record.getOpenid())&&invite.getOpenid().equals(record.getInviteOpenid())){
                    return Result.error("您已使用过兑换码");
                }
            }
            //老用户
            extracted(openid, invite,1);
        }
        inviteMapper.updataReduceCount(invite.getOpenid());
        InviteRecord inviteRecord1 = new InviteRecord();
        inviteRecord1.setOpenid(openid);
        inviteRecord1.setPass(invite.getPass());
        inviteRecord1.setInviteOpenid(invite.getOpenid());
        inviteRecord1.setInviteTime(LocalDateTime.now());
        inviteRecordMapper.save(inviteRecord1);
        return Result.success();
    }

    private void extracted(String openid, Invite invite, Integer count) {
        CouponBook couponBook1 = couponBookMapper.selectByOpenidByCouponId(openid, 2);
        //推荐用户
        CouponBook couponBook2 = couponBookMapper.selectByOpenidByCouponId(invite.getOpenid(), 2);
        if (couponBook1 == null) {
            couponBookMapper.save(CouponBook.builder().couponId(2).count(1).openid(openid).build());
            if (couponBook2 == null) {
                couponBookMapper.save(CouponBook.builder().couponId(2).count(count).openid(invite.getOpenid()).build());
            } else {
                if(count==3){
                    couponBookMapper.updateAddCount3(invite.getOpenid(), 2);
                }else{
                    couponBookMapper.updateAddCount(invite.getOpenid(), 2);
                }
            }
        } else {
            couponBookMapper.updateAddCount(openid, 2);
            if (couponBook2 == null) {
                couponBookMapper.save(CouponBook.builder().couponId(2).count(count).openid(openid).build());
            } else {
                if(count==3){
                    couponBookMapper.updateAddCount3(invite.getOpenid(), 2);
                }else {
                    couponBookMapper.updateAddCount(invite.getOpenid(), 2);
                }
            }
        }
    }

    private static String generateCouponCode() {
        // 使用随机数生成一个长度为10的字符串作为兑换码
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        StringBuilder code = new StringBuilder();

        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            int index = random.nextInt(characters.length());
            code.append(characters.charAt(index));
        }
        return code.toString();
    }
    private  String CheckCode(String pass) {
        Invite invite = inviteMapper.selectByCode(pass);
        if(invite==null){
            return pass;
        }else{
            String str = generateCouponCode();
            CheckCode(str);
        }
        return pass;
    }

}
